package com.ajzhang.facade;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ajzhang.bean.Bill;
import com.ajzhang.bean.UserInfo;

public class BillFacade extends AbstractFacade {

	@Transactional(readOnly = true)
	public List<Bill> findPageByCriteria(int pagerOffset, int pageSizeKey, DetachedCriteria detachedCriteria) throws Exception {
		List list = this.baseDao.findPageByCriteriaOrderBy(detachedCriteria, pagerOffset, pageSizeKey);
		return list;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void save(Bill bill) {
		this.baseDao.insert(bill);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void update(Bill bill) {
		this.baseDao.update(bill);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void delete(Bill bill) {
		this.baseDao.delete(bill);
	}
	
	@Transactional(readOnly = true)
	public List<Bill> findPageTodayByCriteria(int pagerOffset,Date bengTime,Date endTime,UserInfo userInfo) throws Exception {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Bill.class);
		detachedCriteria.add(Expression.gt("createDate", bengTime));
		detachedCriteria.add(Expression.le("createDate", endTime));
		detachedCriteria.add(Expression.eq("userId", userInfo));
		detachedCriteria.addOrder(Order.desc("createDate"));
		return this.baseDao.findPageByCriteriaOrderBy(detachedCriteria, 10, pagerOffset);
	}
	
	@Transactional(readOnly = true)
	public double findPageTodayByCriteria(String Datetime,UserInfo userInfo) throws Exception {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Bill.class);
		Date bengTime = com.ajzhang.util.DateUtil.toDate(Datetime+" 00:00:00");
		Date endTime = com.ajzhang.util.DateUtil.toDate(Datetime+" 23:59:59");
		detachedCriteria.add(Expression.gt("createDate", bengTime));
		detachedCriteria.add(Expression.le("createDate", endTime));
		detachedCriteria.add(Expression.eq("userId", userInfo));
		List<Bill> list = this.baseDao.findByCriteria(detachedCriteria);
		double money = 0;
		if(list.size()!=0){
			for (Bill bill : list) {
				money += bill.getMoney();
			}
			return money;
		}
		return 0;
	}
	
	public Integer findTodayCountByCriteria(Date bengTime,Date endTime,UserInfo userInfo){
		Integer count = 0;
		try {
			DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Bill.class);
			detachedCriteria.add(Expression.gt("createDate", bengTime));
			detachedCriteria.add(Expression.le("createDate", endTime));
			detachedCriteria.add(Expression.eq("userId", userInfo));
			count = this.baseDao.findByCriteriaCount(detachedCriteria);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return count;
	}

}
